import { useRoute } from 'vue-router'
import { getCategoryAPI } from '@/apis/category'
import { watch, onMounted, ref } from 'vue'

export function useCategory() { 
	const route = useRoute()
	const categoryId = route.params.id
	const categoryData = ref({})

	const getCategoryData = async (id) => {
		const res = await getCategoryAPI(id).then((res) => res.data)
		categoryData.value = res.result
	}

	onMounted(() => {
		getCategoryData(categoryId)
	})

	// 参数变化，重新获取数据
	watch(() => route.params.id, (newId, oldId) => {
		getCategoryData(newId)
	})

	return {
		categoryData
	}
}